package com.example.mianshi.mianshiv2;

import java.io.*;

/**
 * 读文件流，将文件一行一行数据添加到队列ConcurrentLinkedQueue中。也可以用LinkedBlonkQuere队列
 */
public class ReadFileThread implements Runnable {

    private DoWorkTemplate template;
    private File file;

    public ReadFileThread(DoWorkTemplate template, File file){
        this.template = template;
        this.file = file;
    }

    @Override
    public void run() {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            String line = "";
            while((line=reader.readLine())!=null){
                if(line == ""){
                    continue;
                }
                MFile mFile = FileFactory.getInstance().createMFile(line.split(","));
                if(mFile != null)
                    template.queue.add(mFile);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(reader != null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            template.countDownLatch.countDown();
        }
    }
}
